'''
Created on Sep 26, 2012

@author: x220
'''
from DifferencesDetector import DifferencesDetector
import cv2
import socket,json
import numpy as np

class BallenBak():
    
    def __init__(self,background):
        self.dd = DifferencesDetector(background)
        
    def get_peoples(self,frame,debug):
        blobs = self.dd.get_contours(frame, threshold_value=30,gray=False,debug=True,kernel=np.ones((3,3),'uint8'),number_of_iterations = 3)
        persons = []
        if(len(blobs) != 0):
            for blob in blobs:
                if(cv2.contourArea(blob) >1000):
                    persons.append(blob)   
        return persons
    
#end class
    
def run(background):
    ch = 0
    bb = BallenBak(background)
    socket_available = False
    
    client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
        client_socket.connect(("localhost", 5000))
        socket_available = True
    except socket.error, (value,message): 
        print("error creating connection")
    
    while(ch != ord('q')):
        try:
            list_values= []
            temp, frame = cam.read()
            people_countours = bb.get_peoples(frame,debug=False)
            if(len(people_countours) != 0):
                for x in people_countours:
                    for cor in x:
                        cord =(cor[0][0],cor[0][1])
                        cv2.line(frame, cord, cord,(0,0,255),1)
                        
                        list_values.append(int(cor[0][0]))
                        list_values.append(int(cor[0][1]))
                if socket_available:
                    client_socket.send(str(json.dumps(list_values))+'\n')
                
            
            cv2.imshow('people', frame)
            ch = 0
            ch = cv2.waitKey(100)
        
        except socket.error, (value,message): 
            print("lost connection")
            ch = ord('q')
        
        
    if socket_available:
        print("Before closing")
        client_socket.send("Quit\n")
        client_socket.shutdown(socket.SHUT_RDWR)
        client_socket.close()   
        print("after closing")

    
     
if __name__ == "__main__":
    '''
    import time
    l = [367, 395, 366, 396, 365, 396, 364, 396, 363, 397, 362, 397, 361, 397, 360, 397, 359, 398, 358, 398, 357, 398, 356, 399, 355, 399, 354, 399, 353, 400, 352, 400, 351, 400, 350, 401, 349, 402, 348, 403, 347, 403, 346, 404, 345, 405, 344, 405, 343, 406, 342, 406, 341, 406, 340, 406, 339, 406, 338, 406, 337, 406, 336, 406, 335, 405, 334, 405, 333, 405, 333, 406, 333, 407, 332, 408, 331, 408, 330, 408, 329, 408, 328, 408, 327, 408, 326, 408, 325, 407, 325, 406, 324, 405, 323, 405, 322, 405, 321, 405, 320, 405, 319, 405, 318, 405, 317, 405, 316, 405, 315, 404, 314, 403, 313, 403, 312, 402, 312, 401, 311, 401, 310, 401, 310, 402, 309, 403, 308, 403, 307, 403, 307, 404, 306, 405, 305, 405, 304, 405, 303, 405, 302, 405, 301, 405, 300, 405, 299, 405, 298, 405, 297, 405, 296, 405, 295, 405, 294, 405, 293, 405, 292, 405, 291, 405, 291, 406, 291, 407, 291, 408, 290, 409, 289, 409, 288, 409, 287, 409, 286, 409, 285, 409, 284, 409, 283, 408, 283, 407, 282, 406, 282, 405, 281, 405, 280, 404, 279, 403, 278, 403, 277, 403, 276, 403, 275, 403, 274, 402, 274, 401, 274, 400, 274, 399, 274, 398, 273, 398, 272, 398, 271, 397, 270, 397, 269, 397, 268, 397, 267, 397, 266, 397, 265, 397, 264, 397, 263, 396, 262, 396, 261, 396, 260, 396, 259, 397, 258, 397, 257, 397, 256, 397, 255, 397, 254, 397, 253, 397, 252, 397, 251, 397, 250, 397, 249, 397, 248, 397, 247, 397, 247, 398, 247, 399, 247, 400, 247, 401, 247, 402, 247, 403, 247, 404, 247, 405, 247, 406, 247, 407, 247, 408, 246, 409, 245, 409, 244, 409, 243, 409, 242, 409, 241, 409, 240, 409, 239, 409, 238, 409, 237, 409, 237, 410, 236, 411, 235, 411, 234, 411, 233, 412, 233, 413, 233, 414, 233, 415, 233, 416, 232, 417, 231, 417, 230, 417, 229, 418, 228, 418, 227, 418, 226, 418, 225, 418, 224, 418, 223, 418, 222, 418, 221, 418, 220, 417, 220, 416, 220, 415, 220, 414, 219, 414, 218, 413, 218, 412, 218, 411, 217, 411, 216, 411, 215, 411, 214, 411, 213, 411, 212, 411, 211, 411, 210, 412, 209, 412, 208, 412, 207, 412, 206, 412, 205, 412, 204, 412, 204, 413, 204, 414, 204, 415, 204, 416, 203, 417, 203, 418, 203, 419, 203, 420, 203, 421, 203, 422, 202, 423, 201, 423, 200, 423, 199, 423, 198, 423, 197, 423, 196, 423, 195, 423, 194, 422, 194, 421, 193, 421, 192, 421, 191, 421, 190, 421, 189, 421, 189, 422, 189, 423, 189, 424, 189, 425, 189, 426, 188, 427, 187, 427, 186, 427, 185, 427, 184, 427, 183, 427, 182, 427, 181, 427, 180, 427, 179, 427, 178, 427, 177, 427, 177, 428, 176, 429, 175, 429, 174, 429, 173, 429, 172, 429, 171, 429, 170, 429, 169, 429, 168, 429, 167, 429, 166, 428, 165, 428, 164, 428, 163, 428, 163, 429, 163, 430, 164, 430, 165, 430, 166, 430, 167, 430, 168, 430, 169, 430, 170, 430, 171, 430, 172, 430, 173, 431, 173, 432, 173, 433, 173, 434, 173, 435, 173, 436, 173, 437, 173, 438, 173, 439, 173, 440, 173, 441, 172, 442, 171, 442, 170, 442, 169, 442, 168, 442, 167, 442, 167, 443, 166, 444, 166, 445, 166, 446, 165, 447, 164, 447, 163, 447, 162, 447, 161, 447, 160, 447, 159, 447, 158, 446, 158, 445, 157, 445, 156, 444, 156, 443, 155, 443, 154, 443, 153, 442, 152, 441, 151, 441, 150, 441, 149, 441, 149, 442, 148, 443, 147, 443, 147, 444, 147, 445, 147, 446, 147, 447, 147, 448, 147, 449, 147, 450, 147, 451, 147, 452, 147, 453, 147, 454, 147, 455, 147, 456, 146, 457, 145, 457, 144, 457, 143, 457, 142, 457, 141, 457, 141, 458, 141, 459, 141, 460, 141, 461, 141, 462, 141, 463, 141, 464, 141, 465, 140, 466, 139, 466, 138, 466, 137, 466, 136, 466, 135, 466, 134, 466, 134, 467, 134, 468, 135, 468, 136, 469, 136, 470, 136, 471, 136, 472, 136, 473, 136, 474, 136, 475, 136, 476, 136, 477, 136, 478, 137, 478, 138, 478, 139, 478, 139, 477, 139, 476, 139, 475, 140, 474, 141, 475, 141, 476, 141, 477, 141, 478, 142, 478, 143, 478, 144, 478, 145, 478, 146, 478, 147, 478, 148, 478, 149, 478, 150, 478, 151, 478, 152, 478, 153, 478, 154, 478, 155, 478, 156, 478, 157, 478, 158, 478, 159, 478, 160, 478, 161, 478, 162, 478, 163, 478, 164, 478, 165, 478, 166, 478, 167, 478, 168, 478, 169, 478, 170, 478, 171, 478, 172, 478, 173, 478, 174, 478, 175, 478, 176, 478, 177, 478, 178, 478, 179, 478, 180, 478, 181, 478, 182, 478, 183, 478, 184, 478, 185, 478, 186, 478, 187, 478, 188, 478, 189, 478, 190, 478, 191, 478, 192, 478, 193, 478, 194, 478, 195, 478, 196, 478, 197, 478, 198, 478, 199, 478, 200, 478, 201, 478, 202, 478, 203, 478, 204, 478, 205, 478, 206, 478, 207, 478, 208, 478, 209, 478, 210, 478, 211, 478, 212, 478, 213, 478, 214, 478, 215, 478, 216, 478, 217, 478, 218, 478, 219, 478, 220, 478, 221, 478, 222, 478, 223, 478, 224, 478, 225, 478, 226, 478, 227, 478, 228, 478, 229, 478, 230, 478, 231, 478, 232, 478, 233, 478, 234, 478, 235, 478, 236, 478, 237, 478, 238, 478, 239, 478, 240, 478, 241, 478, 242, 478, 243, 478, 244, 478, 245, 478, 246, 478, 247, 478, 248, 478, 249, 478, 250, 478, 251, 478, 252, 478, 253, 478, 254, 478, 255, 478, 256, 478, 257, 478, 258, 478, 259, 478, 260, 478, 261, 478, 262, 478, 263, 478, 264, 478, 265, 478, 266, 478, 267, 478, 268, 478, 269, 478, 270, 478, 271, 478, 272, 478, 273, 478, 274, 478, 275, 478, 276, 478, 277, 478, 278, 478, 279, 478, 280, 478, 281, 478, 282, 478, 283, 478, 284, 478, 285, 478, 286, 478, 287, 478, 288, 478, 289, 478, 290, 478, 291, 478, 292, 478, 293, 478, 294, 478, 295, 478, 296, 478, 297, 478, 298, 478, 299, 478, 300, 478, 301, 478, 302, 478, 303, 478, 304, 478, 305, 478, 306, 478, 307, 478, 308, 478, 309, 478, 310, 478, 311, 478, 312, 478, 313, 478, 314, 478, 315, 478, 316, 478, 317, 478, 318, 478, 319, 478, 320, 478, 321, 478, 322, 478, 323, 478, 324, 478, 325, 478, 326, 478, 327, 478, 328, 478, 329, 478, 330, 478, 331, 478, 332, 478, 333, 478, 334, 478, 335, 478, 336, 478, 337, 478, 338, 478, 339, 478, 340, 478, 341, 478, 342, 478, 343, 478, 344, 478, 345, 478, 346, 478, 347, 478, 348, 478, 349, 478, 350, 478, 351, 478, 352, 478, 353, 478, 354, 478, 355, 478, 356, 478, 357, 478, 358, 478, 359, 478, 360, 478, 361, 478, 362, 478, 363, 478, 364, 478, 365, 478, 366, 478, 367, 478, 368, 478, 369, 478, 370, 478, 371, 478, 372, 478, 373, 478, 374, 478, 375, 478, 376, 478, 377, 478, 378, 478, 379, 478, 380, 478, 381, 478, 382, 478, 383, 478, 384, 478, 385, 478, 386, 478, 387, 478, 388, 478, 389, 478, 390, 478, 391, 478, 392, 478, 393, 478, 394, 478, 395, 478, 396, 478, 397, 478, 398, 478, 399, 478, 400, 478, 401, 478, 402, 478, 403, 478, 404, 478, 405, 478, 406, 478, 407, 478, 408, 478, 409, 478, 410, 478, 411, 478, 412, 478, 413, 478, 414, 478, 415, 478, 416, 478, 417, 478, 418, 478, 419, 478, 420, 478, 421, 478, 422, 478, 423, 478, 424, 478, 425, 478, 426, 478, 427, 478, 428, 478, 429, 478, 430, 478, 431, 478, 432, 478, 433, 478, 434, 478, 435, 478, 436, 478, 437, 478, 438, 478, 439, 478, 440, 478, 441, 478, 442, 478, 443, 478, 444, 478, 445, 478, 446, 478, 447, 478, 448, 478, 449, 478, 450, 478, 451, 478, 452, 478, 453, 478, 454, 478, 455, 478, 456, 478, 457, 478, 458, 478, 459, 478, 460, 478, 461, 478, 462, 478, 463, 478, 464, 478, 465, 478, 466, 478, 467, 478, 468, 478, 469, 478, 470, 478, 471, 478, 472, 478, 473, 478, 474, 478, 475, 478, 476, 478, 477, 478, 478, 478, 479, 478, 480, 478, 481, 478, 482, 478, 483, 478, 484, 478, 484, 477, 484, 476, 483, 475, 482, 474, 481, 473, 480, 472, 479, 471, 479, 470, 478, 469, 477, 469, 476, 469, 475, 468, 474, 467, 473, 467, 472, 466, 471, 466, 470, 465, 469, 464, 468, 463, 467, 462, 467, 461, 466, 461, 465, 461, 464, 460, 464, 459, 463, 459, 462, 458, 462, 457, 461, 457, 460, 456, 459, 455, 458, 454, 457, 454, 456, 453, 456, 452, 455, 451, 454, 451, 453, 450, 452, 449, 451, 448, 450, 448, 449, 447, 449, 446, 448, 445, 447, 444, 446, 443, 445, 443, 444, 442, 443, 441, 442, 440, 442, 439, 441, 439, 440, 438, 439, 437, 438, 436, 437, 435, 436, 434, 435, 434, 434, 433, 434, 432, 433, 431, 432, 431, 431, 430, 430, 429, 429, 428, 428, 427, 427, 426, 426, 425, 425, 425, 424, 424, 424, 423, 423, 423, 422, 422, 421, 421, 420, 421, 419, 420, 419, 419, 418, 418, 417, 417, 416, 416, 415, 415, 414, 414, 414, 413, 413, 413, 412, 413, 411, 412, 410, 411, 409, 410, 408, 409, 407, 409, 406, 408, 406, 407, 405, 407, 404, 407, 403, 406, 402, 405, 401, 404, 400, 403, 399, 403, 398, 402, 397, 402, 396, 401, 395, 400, 394, 399, 393, 398, 392, 398, 391, 397, 390, 397, 389, 397, 388, 397, 387, 396, 386, 396, 385, 396, 384, 395, 383, 395, 382, 395, 381, 395, 380, 395, 379, 395, 378, 395, 377, 395, 376, 395, 375, 395, 374, 395, 373, 395, 372, 395, 371, 395, 370, 395, 369, 395, 368, 395]
    client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client_socket.connect(("localhost", 5000))
    while(True):
        print("Send data")
        client_socket.send(str(json.dumps(l))+'\n')
        time.sleep(1)
    '''
    cam = cv2.VideoCapture(1)
    ch = 0
    
    while(ch != ord('q') and ch != ord('g')):
        temp, background = cam.read()
        cv2.imshow('background', background)
        ch = cv2.waitKey(150)
    cv2.destroyWindow('background')
    if(ch == ord('g')):
        run(background)
    else:
        cv2.destroyAllWindows()   
    

